package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.EstateProjectActivityBrandDAO;
import com.ecoolex.ying.space.entity.EstateProjectActivityBrand;
import com.ecoolex.ying.space.entity.EstateProjectActivityBrand.Column;
import com.ecoolex.ying.space.entity.EstateProjectActivityBrandExample;
import com.ecoolex.ying.space.entity.EstateProjectActivityBrandExample.Criteria;

import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EstateProjectActivityBrandManager {
    @Autowired
    private EstateProjectActivityBrandDAO dao;
    
    public EstateProjectActivityBrand get(Integer brandId) {
		return dao.selectByPrimaryKeyWithLogicalDelete(brandId, EstateProjectActivityBrand.NOT_DELETED);
	}

	public Pagination<EstateProjectActivityBrand> query(Integer projectId,String name, Sorting[] sorts, Page page) {
		EstateProjectActivityBrandExample example = new EstateProjectActivityBrandExample();
		Criteria criteria = example.createCriteria();
		if(Check.notNullOrEmpty(name)) {
			criteria.andNameLike("%"+name+"%");
		}
		criteria.andProjectIdEqualTo(projectId).andDeletedEqualTo(EstateProjectActivityBrand.NOT_DELETED);
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());
		example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}
	
	public Pagination<EstateProjectActivityBrand> queryForH5(Integer projectId,Page page) {
		EstateProjectActivityBrandExample example = new EstateProjectActivityBrandExample();
		Criteria criteria = example.createCriteria();
		criteria.andProjectIdEqualTo(projectId).andOnlineEqualTo(true).andDeletedEqualTo(EstateProjectActivityBrand.NOT_DELETED);
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());
		example.orderBy(Column.weight.desc());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}

	public EstateProjectActivityBrand add(EstateProjectActivityBrand add, String createUser){
		add.setOnline(false);
		add.setDeleted(EstateProjectActivityBrand.NOT_DELETED);
		add.setCreateUser(createUser);
		add.setCreateTime(Date.from(Instant.now()));
		add.setUpdateUser(createUser);
		add.setUpdateTime(Date.from(Instant.now()));
		dao.insert(add);
		return add;
	}

	public EstateProjectActivityBrand edit(EstateProjectActivityBrand edit, String updateUser){
		edit.setUpdateUser(updateUser);
		edit.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(edit, Column.excludes(Column.deleted,Column.online,Column.createUser,Column.createTime));
		return edit;
	}

	public void delete(EstateProjectActivityBrand record, String updateUser){
		record.setDeleted(EstateProjectActivityBrand.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.deleted,Column.updateUser,Column.updateTime);
	}

	public void online(EstateProjectActivityBrand record, String updateUser){
		record.setOnline(true);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.online,Column.updateUser,Column.updateTime);
	}

	public void offline(EstateProjectActivityBrand record, String updateUser){
		record.setOnline(false);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.online,Column.updateUser,Column.updateTime);
	}
}